using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration; 
using NUnit.Framework;
using HermesPortal.Core;  
namespace HermesPortal.NUnit
{
    [TestFixture] 
    public class UserServiceFixture
    {

        private  int TESTCREATORID = 1;
        private  int TESTSAGAID = 1;

        [TestFixtureSetUp]
        public void StartUP()
        {
            TESTCREATORID = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["TestCreator"]);
            TESTSAGAID = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["TestSaga"]);
        }
        [Test]
        public void GetCreator()
        {
            Creator testCreator =  UserServices.GetCreator(TESTCREATORID);
            Assert.IsNotNull(testCreator);
            ViewBusinessObjectProperties(testCreator);
        }
        [Test]
        public void SetCreatorRole()
        {
            // save the old role
            SagaRoles oldRole = UserServices.GetCreatorRoleForSaga(TESTCREATORID, TESTSAGAID);
            
            // change role
            UserServices.SetCreatorRoleForSaga(TESTCREATORID, TESTSAGAID, (int)SagaRoles.Browser);
            SagaRoles currentRole = UserServices.GetCreatorRoleForSaga(TESTCREATORID, TESTSAGAID);
            Assert.IsTrue(currentRole == SagaRoles.Browser,String.Format("SagaRole Should be Browser but is {0}", Enum.GetName(typeof(SagaRoles),currentRole)));
            
            // reset to old row
            UserServices.SetCreatorRoleForSaga(TESTCREATORID, TESTSAGAID, (int)oldRole );
            currentRole = UserServices.GetCreatorRoleForSaga(TESTCREATORID, TESTSAGAID);
            Assert.IsTrue(currentRole == oldRole , String.Format("SagaRole Should be Browser but is {0}", Enum.GetName(typeof(SagaRoles), currentRole)));
        }
        private void ViewBusinessObjectProperties(object obj)
        {
            Type objType = obj.GetType();
            PropertyInfo[] props = objType.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                FieldInfo fi = objType.GetField(prop.Name);
                Console.WriteLine(prop.Name + ": " + prop.GetValue(obj, null));
            }
        }

        

    }
}
